package com.dq.utils;

import com.dq.cache.constant.CacheConstant;
import com.dq.cache.J2CacheUtils;
import com.dq.domain.address.SysRegion;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区管理组建
 * @author gewenzhen
 */
public class RegionCacheUtil {

    public static List<SysRegion> sysRegionEntityList = (List<SysRegion>) J2CacheUtils.get(CacheConstant.REGION_CACHE_KEY);

    /**
     * 获取所有国家
     *
     * @return
     */
    public static List<SysRegion> getAllCountry() {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(0)) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取全部省份
     *
     * @return
     */
    public static List<SysRegion> getAllProvice() {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(1)) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取所有城市
     *
     * @return
     */
    public static List<SysRegion> getAllCity() {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(2)) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 根据国家获取全部省份
     *
     * @return
     */
    public static List<SysRegion> getAllProviceByParentId(Integer areaId) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == areaId) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && areaVo.getType().equals(1) && areaId.equals(areaVo.getParent().getId())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取地市
     *
     * @return
     */
    public static List<SysRegion> getChildrenCity(Integer areaId) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == areaId) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && areaVo.getType().equals(2) && areaId.equals(areaVo.getParent().getId())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取地市
     *
     * @return
     */
    public static List<SysRegion> getChildrenCity(String proviceName) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == proviceName) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && areaVo.getType().equals(2) && proviceName.equals(areaVo.getParent()
                        .getName())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取区县
     *
     * @return
     */
    public static List<SysRegion> getChildrenDistrict(Integer areaId) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == areaId) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && areaVo.getType().equals(3) && areaId.equals(areaVo.getParent().getId())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取区县
     *
     * @return
     */
    public static List<SysRegion> getChildrenDistrict(String provinceName, String cityName) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == provinceName || null == cityName) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && areaVo.getType().equals(3)
                        && cityName.equals(areaVo.getParent().getName())
                        && null != getAreaByAreaId(areaVo.getParent().getId())
                        && provinceName.equals(getAreaByAreaId(areaVo.getParent().getId()).getParent().getName())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }


    /**
     * 获取区县
     *
     * @return
     */
    public static List<SysRegion> getChildrenByParentId(Long parentId) {
        List<SysRegion> resultObj = new ArrayList<SysRegion>();
        if (null == parentId) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (null != areaVo.getParent() && parentId.equals(areaVo.getParent().getId())) {
                    resultObj.add(areaVo);
                }
            }
        }
        return resultObj;
    }

    /**
     * 获取区域名称
     *
     * @return
     */
    public static String getAreaNameByAreaId(Long areaId) {
        if (null == areaId) {
            return "";
        }
        String resultObj = areaId.toString();
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getId() == areaId) {
                    resultObj = areaVo.getName();
                    break;
                }
            }
        }
        return resultObj;
    }

    /**
     * 根据Id获取区域
     *
     * @return
     */
    public static SysRegion getAreaByAreaId(Long areaId) {
        SysRegion resultObj = null;
        if (null == areaId) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getId() == areaId) {
                    resultObj = areaVo;
                    break;
                }
            }
        }
        return resultObj;
    }

    /**
     * 根据Id获取区域
     *
     * @return
     */
    public static Long getProvinceIdByName(String areaName) {
        Long resultObj = null;
        if (null == areaName) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(SysRegion.Type.province) && areaVo.getName().equals(areaName)) {
                    resultObj = areaVo.getId();
                    break;
                }
            }
        }
        return resultObj;
    }

    /**
     * 根据Id获取区域
     *
     * @return
     */
    public static Long getCityIdByName(Long provinceId, String areaName) {
        Long resultObj = null;
        if (null == areaName) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(SysRegion.Type.city) && areaVo.getName().equals(areaName)
                        && areaVo.getParent().getId() == provinceId) {
                    resultObj = areaVo.getId();
                    break;
                }
            }
        }
        return resultObj;
    }


    /**
     * 根据Id获取区域
     *
     * @return
     */
    public static Long getDistrictIdByName(Long provinceId, Long cityId, String areaName) {
        Long resultObj = null;
        if (null == areaName) {
            return resultObj;
        }
        if (null != sysRegionEntityList) {
            for (SysRegion areaVo : sysRegionEntityList) {
                if (areaVo.getType().equals(SysRegion.Type.district) && areaVo.getName().equals(areaName)
                        && areaVo.getParent().getId() == cityId
                        && null != getAreaByAreaId(areaVo.getParent().getId())
                        && null != getAreaByAreaId(areaVo.getParent().getId()).getParent()
                        && getAreaByAreaId(areaVo.getParent().getId()).getParent().getId() == provinceId) {
                    resultObj = areaVo.getId();
                    break;
                }
            }
        }
        return resultObj;
    }
}